# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=brotli
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python2-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-brotli-testdata")
pkgver=1.0.4
pkgrel=1
pkgdesc="Brotli compression library (mingw-w64)"
arch=('any')
url='https://github.com/google/brotli'
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-python3" "${MINGW_PACKAGE_PREFIX}-python2")
license=('MIT')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/google/brotli/archive/v${pkgver}.tar.gz")
sha256sums=('2268a3dff1cc36e18549e89a51ee0cd9513908a977d56d6a1f9d4c61c2af37c3')

prepare() {
  cd "$srcdir"/${_realname}-${pkgver}
  for builddir in python{2,3}-build-${CARCH}; do  
    rm -rf $builddir | true
    cp -r "$srcdir/${_realname}-${pkgver}" "$srcdir/${builddir}"
  done
}

build() {
  cd "$srcdir"/${_realname}-${pkgver}
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
      ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done  
  cd "$srcdir"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_{SHARED,STATIC}_LIBS=ON \
      ../${_realname}-${pkgver}

  make
}

check() {
  cd "${srcdir}"/build-${CARCH}
  make test
}

package_brotli() {
  cd "${srcdir}"/build-${CARCH}
  make install DESTDIR="${pkgdir}"
  install -D -m644 "$srcdir"/brotli-$pkgver/LICENSE "$pkgdir${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_python3-brotli() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3" "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/COPYING"

}

package_python2-brotli() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2" "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/COPYING"
}

package_brotli-testdata() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd brotli-$pkgver
  install -dm755 "$pkgdir${MINGW_PREFIX}/share/brotli"
  cp -a tests/testdata "$pkgdir${MINGW_PREFIX}/share/brotli/"
  install -D -m644 "$srcdir"/brotli-$pkgver/LICENSE "$pkgdir${MINGW_PREFIX}/share/licenses/${_realname}-testdata/LICENSE"
}

package_mingw-w64-i686-brotli() {
  package_brotli
}

package_mingw-w64-i686-brotli-testdata() {
  package_brotli-testdata
}

package_mingw-w64-i686-python2-brotli() {
  package_python2-brotli
}

package_mingw-w64-i686-python3-brotli() {
  package_python3-brotli
}

package_mingw-w64-x86_64-brotli() {
  package_brotli
}

package_mingw-w64-x86_64-brotli-testdata() {
  package_brotli-testdata
}

package_mingw-w64-x86_64-python2-brotli() {
  package_python2-brotli
}

package_mingw-w64-x86_64-python3-brotli() {
  package_python3-brotli
}


